/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_impostor_light.h
 * Desc:    Grass impostor light shader
 * Version: 1.01
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	#ifndef FRAGMENT_IMPOSTOR_LIGHT
		#define FRAGMENT_IMPOSTOR_LIGHT
		
		uniform sampler2D s_texture_0;
		uniform sampler2D s_texture_1;
		uniform sampler2D s_texture_2;
		
		uniform half translucent_scale;
		uniform half noise_scale;
		
	#else
		
		#include <core/shaders/default/grass/fragment_impostor_sample.h>
		
		#ifdef WORLD
			half3 light_direction = s_light_direction;
			half3 camera_direction = s_texcoord_2.xyz;
		#else
			half4 light_direction = s_texcoord_2;
			half3 camera_direction = s_texcoord_3.xyz;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			light_direction.xyz *= light_direction_ilength;
		#endif
		
		half3 color = diffuse.xyz * (saturate(dot(light_direction.xyz,normal) * (1.0f - translucent_scale) + translucent_scale) * s_material_shading.x);
		color += getSpecularNormalize(s_material_shading,diffuse.xyz,normal,light_direction.xyz,camera_direction);
		
		#ifndef WORLD
			half attenuation = getAttenuation(light_direction.w - light_direction_length2);
			color *= attenuation;
		#endif
		
	#endif
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	#ifndef FRAGMENT_IMPOSTOR_LIGHT
		#define FRAGMENT_IMPOSTOR_LIGHT
		
		Texture2D s_texture_0 : register(t0);
		Texture2D s_texture_1 : register(t1);
		Texture2D s_texture_2 : register(t2);
		
		cbuffer shader_parameters {
			float translucent_scale;
			#ifdef NOISE
				float noise_scale;
			#endif
		};
		
	#else
		
		#include <core/shaders/default/grass/fragment_impostor_sample.h>
		
		#ifdef WORLD
			half3 light_direction = s_light_direction;
			half3 camera_direction = IN.texcoord_2;
		#else
			half4 light_direction = IN.texcoord_2;
			half3 camera_direction = IN.texcoord_3;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			light_direction.xyz *= light_direction_ilength;
		#endif
		
		half3 color = diffuse.xyz * (saturate(dot(light_direction.xyz,normal) * (1.0f - translucent_scale) + translucent_scale) * s_material_shading.x);
		color += getSpecularNormalize(s_material_shading,diffuse.xyz,normal,light_direction.xyz,camera_direction);
		
		#ifndef WORLD
			half attenuation = getAttenuation(light_direction.w - light_direction_length2);
			color *= attenuation;
		#endif
		
	#endif
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	#ifndef FRAGMENT_IMPOSTOR_LIGHT
		#define FRAGMENT_IMPOSTOR_LIGHT
		
		half translucent_scale;
		half noise_scale;
		
	#else
		
		#include <core/shaders/default/grass/fragment_impostor_sample.h>
		
		#ifdef WORLD
			half3 light_direction = s_light_direction;
			half3 camera_direction = IN.texcoord_2;
		#else
			half4 light_direction = IN.texcoord_2;
			half3 camera_direction = IN.texcoord_3;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			light_direction.xyz *= light_direction_ilength;
		#endif
		
		half3 color = diffuse.xyz * (saturate(dot(light_direction.xyz,normal) * (1.0f - translucent_scale) + translucent_scale) * s_material_shading.x);
		color += getSpecularNormalize(s_material_shading,diffuse.xyz,normal,light_direction.xyz,camera_direction);
		
		#ifndef WORLD
			half attenuation = getAttenuation(light_direction.w - light_direction_length2);
			color *= attenuation;
		#endif
		
	#endif
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	#ifndef FRAGMENT_IMPOSTOR_LIGHT
		#define FRAGMENT_IMPOSTOR_LIGHT
		
		uniform sampler2D s_texture_0 : TEXUNIT0;
		uniform sampler2D s_texture_1 : TEXUNIT1;
		uniform sampler2D s_texture_2 : TEXUNIT2;
		
		uniform half translucent_scale;
		uniform half noise_scale;
		
	#else
		
		#include <core/shaders/default/grass/fragment_impostor_sample.h>
		
		#ifdef WORLD
			half3 light_direction = s_light_direction;
			half3 camera_direction = IN.texcoord_2;
		#else
			half4 light_direction = IN.texcoord_2;
			half3 camera_direction = IN.texcoord_3;
			half light_direction_length2 = dot(light_direction.xyz,light_direction.xyz);
			half light_direction_ilength = rsqrt(light_direction_length2);
			light_direction.xyz *= light_direction_ilength;
		#endif
		
		half3 color = diffuse.xyz * (saturate(dot(light_direction.xyz,normal) * (1.0f - translucent_scale) + translucent_scale) * s_material_shading.x);
		color += getSpecularNormalize(s_material_shading,diffuse.xyz,normal,light_direction.xyz,camera_direction);
		
		#ifndef WORLD
			half attenuation = getAttenuation(light_direction.w - light_direction_length2);
			color *= attenuation;
		#endif
		
	#endif
	
#endif
